package com.ehome.poct.base.dao.device.repository;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.core.util.BeanConvertUtil;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.base.api.dto.device.*;
import com.ehome.poct.base.api.vo.device.TestDeviceClinicVO;
import com.ehome.poct.base.api.vo.device.TestDeviceVO;
import com.ehome.poct.base.dao.device.entity.TestDeviceDO;
import com.ehome.poct.base.dao.device.entity.TestDeviceOperateDO;
import com.ehome.poct.base.dao.device.mapper.TestBloodDeviceParamNewMapper;
import com.ehome.poct.base.dao.device.mapper.TestBloodDeviceParamRecordMapper;
import com.ehome.poct.base.dao.device.mapper.TestDeviceMapper;
import com.ehome.poct.base.dao.device.mapper.TestDeviceOperateMapper;
import com.ehome.poct.base.device.repository.AbstractDeviceRepository;
import com.ehome.poct.base.device.repository.TestDeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 设备表RepositoryImpl
 *
 * @since 瑶苏
 * @Date 2022-03-03 09:23
 */
@Repository
public class TestDeviceRepositoryImpl extends AbstractCrudRepository<TestDeviceDO, TestDeviceDTO> implements TestDeviceRepository {
    @Autowired
    private TestDeviceMapper testDeviceMapper;
    @Autowired
    List<AbstractDeviceRepository> abstractDeviceRepositories;

    @Autowired
    TestDeviceOperateMapper testDeviceOperateMapper;
    @Autowired
    TestBloodDeviceParamNewMapper testBloodDeviceParamNewMapper;
    @Autowired
    TestBloodDeviceParamRecordMapper testBloodDeviceParamRecordMapper;

    @Override
    public TestDeviceVO getBySno(String sno) {
        return BeanConvertUtil.convertBean( testDeviceMapper.selectOne(Wrappers.<TestDeviceDO>lambdaQuery()
        .eq(TestDeviceDO::getSno,sno).last(" limit 1")),TestDeviceVO.class);
    }
    
    @Override
    public List<TestDeviceClinicVO> getDeviceBySno(TestDeviceClinicDTO testDeviceClinicDTO) {
        return testDeviceMapper.getDeviceBySno(testDeviceClinicDTO);
    }
    
    /**
     * 校验是否存在这个设备编号
     *
     * @param sno 设备号
     * @return true(已存在)/false(不存在)
     */
    @Override
    public Boolean isExistence(String sno) {
        //查询表中设备号是否存在
        Integer count = testDeviceMapper.selectCount(Wrappers.<TestDeviceDO>lambdaQuery()
                .eq(TestDeviceDO::getSno, sno)
        );
        if (count > 0) {
            //(设备号存在)
            return true;
        }
        //(设备号不存在)
        return false;
    }
    
    @Override
    public Boolean addDevice(String sno, Integer deviceType) {
        //操作记录
        TestDeviceOperateDO testDeviceOperate = new TestDeviceOperateDO();
        // 操作人
//        OauthUser user = SecurityUtils.getUser();
        testDeviceOperate.setSno(sno);
        testDeviceOperate.setOperateType("设备编号创建");
        testDeviceOperate.setContent(sno + "操作人" + "");

        testDeviceOperate.insert();
        //添加设备
        abstractDeviceRepositories.forEach(r -> {
            if (r.deviceType(deviceType)) {
                r.addDevice(sno);
            }
        });
        return true;
    }
    
    @Override
    public void updateStatus(DeviceStatusDTO dto) {
        TestDeviceDO testDeviceDO = testDeviceMapper.selectOne(Wrappers.<TestDeviceDO>lambdaQuery()
                .eq(TestDeviceDO::getSno, dto.getSno())
                .orderByDesc(TestDeviceDO::getCreateTime)
                .last("limit 1"));
        testDeviceDO.setStatus(dto.getStatus());
        testDeviceMapper.updateById(testDeviceDO);
    }
    
    @Override
    public Boolean isBind(String sno, Long clinicId) {
        TestDeviceDO testDeviceDO = testDeviceMapper.selectOne(Wrappers.<TestDeviceDO>lambdaQuery()
                .eq(TestDeviceDO::getSno, sno)
                .eq(TestDeviceDO::getTermClinicId, clinicId)
                .orderByDesc(TestDeviceDO::getCreateTime)
                .last("limit 1"));
        if (ObjectUtil.isEmpty(testDeviceDO)) {
            return false;
        }
        return true;
    }
    
    @Override
    public List<TestDeviceVO> getByClinic(Long clinicId) {
        List<TestDeviceDO> testDeviceDOs = testDeviceMapper.selectList(Wrappers.<TestDeviceDO>lambdaQuery()
                .eq(TestDeviceDO::getTermClinicId, clinicId));
        return BeanConvertUtil.convertBeanList(testDeviceDOs, TestDeviceVO.class);
    }
    
    @Override
    public TestDeviceVO getByClinic(Long clinicId, String sno, Integer deviceType) {
        TestDeviceDO testDeviceDO = testDeviceMapper.selectOne(Wrappers.<TestDeviceDO>lambdaQuery()
                .eq(TestDeviceDO::getTermClinicId, clinicId)
                .eq(TestDeviceDO::getSno, sno)
                .eq(TestDeviceDO::getDeviceType, deviceType));
        //没有绑定的，或者解绑了的，不应该返回设备信息
        if (testDeviceDO.getStatus() != 1 && testDeviceDO.getStatus() != 5 ){
            return BeanConvertUtil.convertBean(testDeviceDO, TestDeviceVO.class);
        }
        return null;
    }
    
    @Override
    public TestDeviceVO getBySnoAndType(String sno, Integer deviceType) {
       return  BeanConvertUtil.convertBean( testDeviceMapper.selectOne(Wrappers.<TestDeviceDO>lambdaQuery()
                .eq(TestDeviceDO::getSno, sno)
                .eq(TestDeviceDO::getDeviceType, deviceType)),TestDeviceVO.class);
    }

    @Override
    public Integer bindDeviceCount(TestVersionRelationQry qry) {
        /**
         * 绑定设备数
         */
        Integer count = testDeviceMapper.selectCount(Wrappers.<TestDeviceDO>query()
                .eq(ObjectUtil.isNotEmpty(qry.getDeviceType()), "device_type", qry.getDeviceType())
                .eq("`status`", 1));
        return count;
    }

    @Override
    public List<Long> getAllBindClinic() {
        return testDeviceMapper.getAllBindClinic();
    }

    @Override
    public TestDeviceVO getByClinicAndType(Long  clinicId, Integer deviceType) {
        return  BeanConvertUtil.convertBean( testDeviceMapper.selectOne(Wrappers.<TestDeviceDO>lambdaQuery()
                .eq(TestDeviceDO::getTermClinicId, clinicId)
                .eq(TestDeviceDO::getDeviceType, deviceType)),TestDeviceVO.class);
    }
}
